
// FolderManagerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FolderManager.h"
#include "FolderManagerDlg.h"
#include "FolderManageDef.h"
#include "afxdialogex.h"
#include "..\Listing\Listing.h"
#include "..\Deteting\Deteting.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFolderManagerDlg dialog




CFolderManagerDlg::CFolderManagerDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CFolderManagerDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFolderManagerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LISTFORDEL, m_lbxLstFiles);
}

BEGIN_MESSAGE_MAP(CFolderManagerDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNBROWSER, &CFolderManagerDlg::OnBnClickedBtnbrowser)
	ON_BN_CLICKED(IDC_BTNLISTING, &CFolderManagerDlg::OnBnClickedBtnlisting)
	ON_BN_CLICKED(IDC_BTNDEL, &CFolderManagerDlg::OnBnClickedBtndel)
	ON_BN_CLICKED(IDC_BTNDELALL, &CFolderManagerDlg::OnBnClickedBtndelall)
END_MESSAGE_MAP()


// CFolderManagerDlg message handlers

BOOL CFolderManagerDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_nCountFile	= 0;
	m_nCountFolder	= 0;
	memset(m_szDisplayName, NULL, sizeof(m_szDisplayName));
	SetDlgItemText(IDC_EDITLISTING, LISTING_INI);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFolderManagerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFolderManagerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CFolderManagerDlg::OnBnClickedBtnbrowser()
{
	// TODO: Add your control notification handler code here
	BOOL bRet        = FALSE;

	ZeroMemory(&m_bi, sizeof(m_bi));
	m_szDisplayName[0]	= ' ';
	m_bi.hwndOwner      = NULL;
	m_bi.pidlRoot       = NULL;
	m_bi.pszDisplayName = m_szDisplayName;
	m_bi.lpszTitle		= TITLE_OPEN;
	m_bi.ulFlags        = BIF_RETURNONLYFSDIRS;
	m_bi.lParam         = NULL;
	m_bi.iImage         = 0;

	LPITEMIDLIST pidl   = SHBrowseForFolder(&m_bi);
	if (NULL != pidl)
	{
		bRet = SHGetPathFromIDList(pidl, m_szDisplayName);
		if (FALSE == bRet)
			return;
		SetDlgItemText(IDC_EDITPATH, m_szDisplayName);
	}
}

void CFolderManagerDlg::OnBnClickedBtnlisting()
{
	// TODO: Add your control notification handler code here
	BOOL	bResultGet = FALSE;
	TCHAR	szResultListing[MAX_PATH];
	DWORD	nTimeListing = 0;

	GetDlgItemText(IDC_EDITPATH, m_szDisplayName, MAX_PATH);
	if(0 == wcscmp(m_szDisplayName, EMPTY_STRING))
	{
		MessageBox(INVALID_PATH, ERROR_PATH, MB_OK | MB_ICONERROR);
		return;
	}

	m_lbxLstFiles.ResetContent();
	m_lbxLstFiles.SetHorizontalExtent(HOR_SCOLL_RANGE);
	m_szaLstFile.RemoveAll();

	nTimeListing = GetTickCount();
	GetDlgItem(IDC_BTNLISTING)->EnableWindow(FALSE);
	bResultGet = GetListRecursiveX(m_szDisplayName, &m_szaLstFile, &m_nCountFolder, &m_nCountFile);
	if(FALSE == bResultGet)
	{
		MessageBox(INVALID_PATH, ERROR_GETLIST, MB_OK | MB_ICONERROR);
		GetDlgItem(IDC_BTNLISTING)->EnableWindow(TRUE);
		return;
	}
	for (int i = 0; i < m_szaLstFile.GetSize() ; i++)
	{
		m_lbxLstFiles.AddString(m_szaLstFile.GetAt(i));
	}
	nTimeListing = GetTickCount() - nTimeListing;

	wsprintf(szResultListing, LISTING_RES, m_nCountFolder, m_nCountFile, nTimeListing);
	SetDlgItemText(IDC_EDITLISTING, szResultListing);
	GetDlgItem(IDC_BTNLISTING)->EnableWindow(TRUE);
}

void CFolderManagerDlg::OnBnClickedBtndel()
{
	// TODO: Add your control notification handler code here
	TCHAR pszDeleteFile[MAX_PATH] = {0};
	int nSeletedItem = m_lbxLstFiles.GetCurSel();

	if (LB_ERR == nSeletedItem)
	{
		MessageBox(REQUEST_SELETE_ITEM, ERROR_SELETED_ITEM, MB_OK | MB_ICONERROR);
		return;
	}
	if (LB_ERR == m_lbxLstFiles.GetText(nSeletedItem, pszDeleteFile))
	{
		MessageBox(INVALID_SELETE_ITEM, ERROR_SELETED_ITEM, MB_OK | MB_ICONERROR);
		return;
	}
	if (FALSE == DeleteSeletedFile(pszDeleteFile))
	{
		MessageBox(FILE_UNDELETE, ERROR_DELETE_FILE, MB_OK | MB_ICONERROR);
		return;
	}
	m_lbxLstFiles.DeleteString(nSeletedItem);
}

void CFolderManagerDlg::OnBnClickedBtndelall()
{
	// TODO: Add your control notification handler code here
	
}
